package com.ikas.autumn.iec104.common.task;

import com.lmax.disruptor.RingBuffer;
import lombok.extern.slf4j.Slf4j;

/**
 * @version V1.0
 */
@Slf4j
public class TaskEventProducer {
    private final RingBuffer<TaskEvent> ringBuffer;

    public TaskEventProducer(RingBuffer<TaskEvent> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }

    public void doTask(Task task) {
        // 获取下一个序号
        long sequence = ringBuffer.next();
        try {
            // 写入数据
            TaskEvent emailEvent = ringBuffer.get(sequence);
            emailEvent.setTask(task);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            // 通知消费者该资源可以消费了
            ringBuffer.publish(sequence);
        }
    }
}
